module SurveyHelper
	
	
 	def add_question_link(form_builder)
 		 link_to_function 'Dodaj pytanie' do |page|
      	 	form_builder.fields_for :questions, Question.new, :child_index => 'NEW_RECORD' do |f|
      	 				STDERR.puts "-----------------------------------------------------xxx"
	
         		htmlOpen = render(:partial => 'openquestion', :locals => { :form => f })
         		htmlClosed = render(:partial => 'closedquestion', :locals => { :form => f })
         	
         		
         #		if params[:type]==10
         #			html = render(:partial => 'question', :locals => { :form => f })
         #		elsif params[:type]==11	
         #			html = render(:partial => 'question', :locals => { :form => f })
         #		else  
         #			html = render(:partial => 'question', :locals => { :form => f })
     	#		end
     			page << "var selectvar = document.getElementById('type');"		
     			page << "var dropdownIndex = selectvar.selectedIndex;"
     			page << "var value = selectvar.options[dropdownIndex].text;"	
     		#	page << "window.alert(value);"
	
     		#	page.insert_html :bottom, ('questions'), "<li>$('dropdownIndex')</li>"
     		#	page << "var testVar =23;"
 			#	page << "window.alert(value);"
				page << "var timex=new Date().getTime();"
     			page << "if(value=='open question'){"
     		 	page << "$('questions').insert({ bottom: '#{escape_javascript(htmlOpen)}'.replace(/NEW_RECORD/g, timex) });"
       			page << "addPresenceValidation('survey_questions_attributes_'+timex+'_question');
       					}"
     		 	page << "else if(value=='closed question'){"
     		 	page << "$('questions').insert({ bottom: '#{escape_javascript(htmlClosed)}'.replace(/NEW_RECORD/g,timex) });"
     			page << "addPresenceValidation('survey_questions_attributes_'+timex+'_question');"
     			page << "addNumericalityValidation('survey_questions_attributes_'+timex+'_closedquestion_attributes_minAnswers');"
     			page << "addNumericalityValidation('survey_questions_attributes_'+timex+'_closedquestion_attributes_maxAnswers');"			
     				
     			page << "addPresenceValidation('survey_questions_attributes_'+timex+'_closedquestion_attributes_cqpossibleanswers_attributes_0_possibleAnswer');				
     			
     			
     			}"
	     		 	
        	
    	 		end
    	 end
   end
   
   def add_answer_link(form_builder)
 		 link_to_function 'Dodaj odpowiedź' do |page|
      	 	form_builder.fields_for :cqpossibleanswers, CQPossibleAnswer.new, :child_index => 'NEW_RECORD' do |f|
      	 				STDERR.puts "-----------------------------------------------------xxx"
			
         		html = render(:partial => 'closedanswer', :locals => { :form => f }) 				
     		 	page << "$('questions').insert({ bottom: '#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()) });"
       
        	 	
    	 		end
    	 end
   end
   
   
   def add_possible_answer_link
   	 	link_to_function 'Dodaj odpowiedź' do |page|
   	 #		page << "var value =this.parentNode.childNodes[1].id;"
   	 #		page << "var newdiv = document.createElement('div');"
   	 #		page << "newdiv.innerHTML='<input type='text' name='uha'>';"
		#	page << "this.parentNode.appendChild(newdiv);"
		#   page <<"var eldocument.forms[0];"	 	
		#	page << "window.alert();"
		 		
		page << "var value =this.parentNode.childNodes[1].id;"
		page << "var value2 =this.parentNode.childNodes[1].name;"
	
		@str= "var str="+"'"+"\\'"+"';"

		page << "var p = document.createElement('p');"
		page << "this.parentNode.appendChild(p);"
		page << "var el = document.createElement('input');"
		page << "el = this.parentNode.appendChild(el);"
		page << "el.id = value.replace('0_possibleAnswer', new Date().getTime() )+'_possibleAnswer';"
		page << "el.name=value2.replace('[0][possibleAnswer]', '['+new Date().getTime()+'][possibleAnswer]' );";
		page << "el.type = 'text';"
		page << "el.value = '';"
		page << "el.size = '30';"
   		page << "addPresenceValidation(el.id)"
		


 		
   	 	end
  
   end
   
   
   def get_class_name_on_ready survey
	   if (survey.ready=="y") 
		classname="ready_survey" 
	   else 
	   	classname="not_ready_survey"
	   end	   	
   end
   
  # question->this.parentNode.childNodes[3].childNodes[3].id;;var p = document.createElement('p');x.appendChild(p); "
  #	link_to_function("aa", "var x=this.parentNode.childNodes[5].id;  hidden
  #var x=this.parentNode.childNodes[7].childNodes[3].id
  #var x=this.parentNode.childNodes[9].childNodes[3].id
  
   def remove_cq_question_link(form_builder)
 
     if form_builder.object.new_record?
    #    If the task is a new record, we can just remove the div from the dom
         link_to_function("Usuń pytanie", "disableCQValidation(this);(this).up('.question').remove();"			 	
       	);
     else
       # However if it's a "real" record it has to be deleted from the database,
       # for this reason the new fields_for, accept_nested_attributes helpers give us _delete,
       # a virtual attribute that tells rails to delete the child record.
       form_builder.hidden_field(:_delete) +
       link_to_function("Usuń pytanie", "$(this).up('.question').hide(); $(this).previous().value = '1';")
     end
   end 
   def remove_oq_question_link(form_builder)
 
     if form_builder.object.new_record?
    #    If the task is a new record, we can just remove the div from the dom
         link_to_function("Usuń pytanie", "disableOQValidation(this);(this).up('.question').remove();"			 	
       	);
     else
       # However if it's a "real" record it has to be deleted from the database,
       # for this reason the new fields_for, accept_nested_attributes helpers give us _delete,
       # a virtual attribute that tells rails to delete the child record.
       form_builder.hidden_field(:_delete) +
       link_to_function("Usuń pytanie", "$(this).up('.question').hide(); $(this).previous().value = '1';")
     end
   end 
  
end


















